<template>
    <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
            <h2 class="text-center  margin-bottom" >{{title}}</h2>
            <a-form layout="inline" @keyup.enter.native="searchQuery">
                <a-row :gutter="24">
                    <a-col :md="6" :sm="24">
                        <a-form-item label="机构名称">
                            <a-input v-model="queryParam.customerName" placeholder="请选择机构名称"/>
                        </a-form-item>
                    </a-col>

                    <a-col :md="6" :sm="24">
                        <a-form-item label="过期状态">
                            <a-select placeholder="请选择过期状态" v-model="queryParam.state">
<!--                                <a-select-option value="">全部</a-select-option>-->
                                <a-select-option value="1">即将过期</a-select-option>
                                <a-select-option value="2">已过期</a-select-option>
                            </a-select>
                        </a-form-item>
                    </a-col>

                    <a-col :md="6" :sm="24">
                        <a-form-item label="过期天数" >
                            <a-input-number :min="0" :precision="0"  v-model="queryParam.endDay" />
                        </a-form-item>
                    </a-col>

                    <a-col :md="12" :sm="24">
                        <a-form-item label="区域查询">
                            <select-area ref="selectAreaRef" @change="changeArea" ></select-area>
                        </a-form-item>
                    </a-col>

                    <!--<a-col :xl="6" :lg="7" :md="8" :sm="24">-->
                        <!--<span class="table-page-search-submitButtons">-->
                          <!--<a-button type="primary" @click="searchQuery" icon="search">查询</a-button>-->
                        <!--</span>-->
                    <!--</a-col>-->

                </a-row>
            </a-form>
        </div>
        <!-- 查询区域-END -->
        <div class="table-operator" style="display: flex;justify-content: space-between;align-items: center;">
            <div>
                <a-button type="primary" @click="searchQuery" icon="search">查询数据列表</a-button>
                <a-button type="primary" @click="resetForm">重置查询</a-button>
            </div>
            <div>
                <a-button type="primary" icon="printer" v-print="'#printTableList'">打印</a-button>
                <a-button type="primary" icon="download" @click="handleMwExportXlsForImages">
                    导出Excel
                </a-button>
            </div>
        </div>

        <!-- table区域-begin -->
        <div>
            <!--<div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">-->
                <!--<i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择-->
                <!--<a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项-->
                <!--<a style="margin-left: 24px" @click="onClearSelected">清空</a>-->

            <!--</div>-->

            <!--:rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"-->
            <a-table
                    ref="table"
                    size="middle"
                    bordered
                    rowKey="id"
                    :columns="columns"
                    :dataSource="dataSource"
                    :pagination="ipagination"
                    :loading="loading"
                    class="j-table-force-nowrap"
                    @change="handleTableChange"
            >
                <template slot="htmlSlot" slot-scope="text">
                    <div v-html="text"></div>
                </template>
                <template slot="imgSlot" slot-scope="text">
                    <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
                    <img v-else :src="text" height="25px" alt style="width: auto;height: 50px;"/>
                </template>
                <template slot="fileSlot" slot-scope="text">
                    <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
                    <a-button
                            v-else
                            :ghost="true"
                            type="primary"
                            icon="download"
                            size="small"
                            @click="uploadFile(text)"
                    >下载
                    </a-button>
                </template>
            </a-table>
        </div>


        <div v-show="false">
            <div id="printTableList">
                <h2 style="text-align: center;font-weight: bold;">
                    {{title}}
                </h2>
                <h3>打印时间 ： {{new Date().FormatDate()}}</h3>
                <a-table
                        size="small"
                        bordered
                        rowKey="id"
                        :columns="columns"
                        :dataSource="dataSource"
                        :pagination="false"
                >
                    <template slot="htmlSlot" slot-scope="text">
                        <div style="color: black !important;" v-html="text"></div>
                    </template>
                </a-table>
            </div>
        </div>

    </a-card>
</template>

<script>
    /**
     * create by Jane
     * 机构合同到期预警
     */
    import '@/assets/less/TableExpand.less'
    import { mixinDevice } from '@/utils/mixin'
    import { JeecgListMixin } from '@/mixins/JeecgListMixin'
    import SelectArea from '@/components/Jane/SelectArea'
    import { downloadFile } from '@/api/manage' // 模糊下拉查询

    export default {
        mixins: [JeecgListMixin, mixinDevice],
        components: {
            SelectArea,
        },
        data() {
            return {
                title : '机构合同到期预警列表',
                description: '机构合同到期预警列表',
                fiveWeightAndBag: {},
                // 表头
                columns: [
                    {
                        title: '序号',
                        dataIndex: '',
                        key: 'rowIndex',
                        width: 60,
                        align: 'center',
                        customRender: function(t, r, index) {
                            return parseInt(index) + 1
                        }
                    },
                    {
                        title: '机构编码',
                        align: 'center',
                        dataIndex: 'customerNumber'
                    },
                    {
                        title: '机构名称',
                        align: 'center',
                        dataIndex: 'customerName'
                    },
                    {
                        title: '过期日期',
                        align: 'center',
                        dataIndex: 'contractEnd',
                        scopedSlots: { customRender: 'htmlSlot' },
                        customRender: function(value, row, index) {
                            const obj = {
                                children: '',
                                attrs: {}
                            }
                            let time1 = new Date(value).getTime();
                            let time2 = new Date().getTime();

                            if (time1 < time2) {
                                let day = parseInt((time2-time1)/ (1000 * 60 * 60 * 24));
                                obj['children'] = <span style="color: red;" >已过期{day}天</span>;
                            }
                            if (time1 > time2) {
                                let day = parseInt((time1-time2)/ (1000 * 60 * 60 * 24)) + 2;
                                obj['children'] = <span style="color: deepskyblue;" >剩余{day}天</span>;
                            }
                            return obj
                        }
                    },
                    {
                        title: '负责人',
                        align: 'center',
                        dataIndex: 'leadername',
                        customRender: function(value, row, index) {
                            return value || ' '
                        }
                    },
                    {
                        title: '负责人电话',
                        align: 'center',
                        dataIndex: 'leaderphone',
                        customRender: function(value, row, index) {
                            return value || ' '
                        }
                    },
                    // {
                    //     title: '第二手机号',
                    //     align: 'center',
                    //     dataIndex: 'secondphone',
                    //     customRender: function(value, row, index) {
                    //         return value || ' '
                    //     }
                    // },
                    {
                        title: '机构地址',
                        align: 'center',
                        dataIndex: 'address',
                        customRender: function(value, row, index) {
                            return value || ' '
                        }
                    },
                ],
                url: {
                    list: '/hc/hcContract/getContractList',   // 小彭接口 3.0 : 查询数据列表
                    exportXlsUrl: '/hc/hcContract/exportContractList'  // 小董接口
                },
                dictOptions: {},
                queryParam : {
                    state : '1',
                    endDay: '30',
                },

            }
        },
        // computed: {
        //     importExcelUrl: function() {
        //         return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
        //     }
        // },
        mounted() {
        },
        methods: {
            // 初始化数据
            initDictConfig() {
                for (let i in this.queryParam) {
                    this.queryParam[i] = ''
                }
                // 获取到用户登陆的信息
                let userInfo = this.$store.getters.userInfo;
                if(userInfo.cityCode){
                    this.queryParam.cityCode = userInfo.cityCode;
                }
                if(userInfo.areaCode){
                    this.queryParam.areaCode = userInfo.areaCode;
                }
                let ref = this.$refs.selectAreaRef;
                if(ref && ref.clearSelectArea != undefined){
                    ref.clearSelectArea();
                }
                // 过期状态
                this.queryParam.state = '1';
                // 过期天数
                this.queryParam.endDay = 30;

            },
            async changeArea({ area,city }){
                if(city){
                    this.queryParam.cityCode = city;
                }
                if(area){
                    this.queryParam.areaCode = area;
                }
            },
            searchQuery: function() {
                this.selectedRowKeys = []
                this.selectionRows = []
                this.loadData(1)
            },
            resetForm: function() {
                this.queryParam.customerName = ''
                this.initDictConfig()
                this.searchQuery()
            },

            handleMwExportXlsForImages() {
                let param = { ...this.queryParam }
                // if (this.selectedRowKeys && this.selectedRowKeys.length != 1) {
                //     this.$message.warning('请仅选择一家机构导出！')
                //     return
                // }
                // param.customerName = this.selectionRows[0].customerName
                let name = `合同到期列表${new Date().FormatDate()}.xls`;
                downloadFile(this.url.exportXlsUrl, name, param)

            }

        }
    }
</script>
<style scoped>
    @import '~@assets/less/common.less';
</style>
